package cn.org.eshow.service.impl;

import cn.org.eshow.bean.query.OrderItemQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.OrderItemDao;
import cn.org.eshow.model.OrderItem;
import cn.org.eshow.model.Product;
import cn.org.eshow.model.ProductComment;
import cn.org.eshow.model.ProductOrder;
import cn.org.eshow.service.OrderItemManager;
import cn.org.eshow.service.ProductCommentManager;
import cn.org.eshow.service.ProductManager;
import cn.org.eshow.service.ProductOrderManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.Date;
import java.util.List;

@WebService(serviceName = "OrderItemService", endpointInterface = "cn.org.eshow.service.OrderItemManager")
@Service
public class OrderItemManagerImpl extends GenericManagerImpl<OrderItem, Integer> implements OrderItemManager {

    @Autowired
    OrderItemDao orderItemDao;
    @Autowired
    ProductCommentManager productCommentManager;
    @Autowired
    ProductOrderManager productOrderManager;
    @Autowired
    ProductManager productManager;

    public OrderItemManagerImpl() {
    }

    @Autowired
    public OrderItemManagerImpl(OrderItemDao orderItemDao) {
        super(orderItemDao);
        this.orderItemDao = orderItemDao;
    }

    @Override
    public List<OrderItem> list(OrderItemQuery query) {
        return orderItemDao.list(query);
    }

    @Override
    public List<OrderItem> list(Integer productOrderId) {
        OrderItemQuery query = new OrderItemQuery();
        query.setProductOrderId(productOrderId);
        return orderItemDao.list(query);

    }

    @Override
    public Page<OrderItem> search(OrderItemQuery query) {
        return orderItemDao.search(query);
    }

    @Override
    public OrderItem check(OrderItemQuery query) {
        List<OrderItem> list = orderItemDao.list(query);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void comment(OrderItem orderItem, ProductComment productComment) {
        if(orderItem.getIfComment()){
            return;
        }
        orderItem.setIfComment(true);
        orderItem=orderItemDao.save(orderItem);
        ProductOrder productOrder=orderItem.getProductOrder();

        Product product=orderItem.getProduct();
        if(product.getCount()==null || product.getCount()==0){
            product.setCount(0);
        }
        product.setCount(product.getCount()+1);//评论数+1
        product=productManager.save(product);

        productComment.setProduct(product);
        productComment.setProductItem(orderItem.getProductItem());
        productComment.setEnabled(true);
//        productComment.setShop(product.getShop());
        productComment.setUser(orderItem.getUser());
        productComment.setProductOrder(orderItem.getProductOrder());
        productComment.setOrderItem(orderItem);
        productComment=productCommentManager.save(productComment);

        //判断订单明细是否都已经评价过了
        List<OrderItem> orderItems=this.list(new OrderItemQuery(productOrder.getId(),false));
        if(orderItems.isEmpty()){
            productOrder.setUpdateTime(new Date());
            productOrder.setCommentState(1);//订单已评价
            productOrder.setStatements("已评价");
            productOrder=productOrderManager.save(productOrder);

        }

    }

}